package com.dofast.bt.mvp.presenter.basket;

import com.dofast.bt.adapter.PaymentAdapter;
import com.dofast.bt.base.BaseMvpPresenter;
import com.dofast.bt.bean.AddressBean;
import com.dofast.bt.bean.ShoppingCartBean;
import com.dofast.bt.mvp.view.activity.basket.PaymentView;
import com.dofast.bt.util.Helper;

import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * username:Created by hsj on 2016/12/18 0018.
 * class：
 * 类描述：确认付款
 */

public class PaymentPresenter extends BaseMvpPresenter<PaymentView> implements IPaymentPresenter {
    private List<ShoppingCartBean> list = null;
    private AddressBean address = null;
    private PaymentAdapter adapter = null;

    @Override
    public void initData() {
        checkViewAttach();
        list = new ArrayList<ShoppingCartBean>();
        address = new AddressBean();
        list = mvpView.getGoods();
        totleNum();
        address = mvpView.getAddress();
        adapter = new PaymentAdapter(list);
        mvpView.initAdapter(adapter);
        mvpView.setAddress(address);

    }

    @Override
    public void pay() {
        Helper.showToast("该功能还未开放，敬请期待新版本！");
    }
    private void totleNum(){
        try {
            double sum = 0;
            for (int i = 0; i < list.size(); i++) {
                int num = list.get(i).getNum();
                double price = list.get(i).getGoods().getPrice();
                sum += num*price;
            }
            mvpView.setAllMoney(sum + "");
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void showDialog() {

    }

    /**
     * @param list
     * @return 总金额
     */
    private double showAllMoney(List<ShoppingCartBean> list) {
        double i = 0.0;
        if (null == list || list.isEmpty()) {
            return i;
        }
        try {
            for (ShoppingCartBean shoppingCartBean : list) {
                i += shoppingCartBean.getGoods().getPrice() * shoppingCartBean.getNum();
            }

        } catch (DbException e) {
            e.printStackTrace();
        }
        return i;
    }
}
